package org.mg.frontend.security;

import org.mg.core.entity.User;
import org.mg.core.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * @author MG
 * @since 06-04-2013
 */
public class AuthUserService implements UserDetailsService {
    Logger logger = LoggerFactory.getLogger(AuthUserService.class);

    UserService userService;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        User user;
        try {
            user = userService.loadUserByUsername(userName);
        } catch (Exception e) {
            logger.warn("User not found for username: {0}", userName);
            throw new UsernameNotFoundException("User not found", e);
        }
        return new AuthUserDetails(user);
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
